package com.example.newspublish.controller;

import com.example.newspublish.pojo.dto.UserDTO;
import com.example.newspublish.pojo.form.LoginForm;
import com.example.newspublish.pojo.form.RegisterForm;
import com.example.newspublish.service.UserService;
import com.example.newspublish.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Tag(name = "用户管理")
@CrossOrigin
@RestController
@RequestMapping("/sysUser/user")
public class UserController {
     @Autowired
     UserService userService;

     @Operation(summary = "登录")
     @PostMapping("/login")
     public Result login(@RequestBody LoginForm loginForm) throws Exception {
          System.out.println("登录请求控制层调用");
//          try {
               return userService.login(loginForm.getUsername(),loginForm.getPassword());
//          } catch (Exception e) {
//               throw new Exception("发生系统错误");
//          }
//        return userService.login(username,password);
     }

     @Operation(summary = "注册")
     @PostMapping("/register")
     public Result register(@RequestBody RegisterForm registerForm) {
        return userService.register(registerForm);
     }

     @Operation(summary = "修改用户信息",security = {@SecurityRequirement(name = "Authorization")})
     @PostMapping("/update")
     public Result updateInfo(@RequestBody UserDTO userDTO) {
          log.debug("修改用户信息：{}", userDTO);
          userService.updateInfo(userDTO);

          return Result.success();
     }

     @Operation(summary = "退出登录")
     @PostMapping("/logout")
     public Result<String> logout() {
          log.debug("用户已经退出登录....");
          return Result.success();
     }


}
